var dates,valueId,minDate,maxDate,whereId,valueDate,fromTrue,falg;
var curDate = new Date();
function createDiv(){
    var oDiv=document.createElement("div");
    oDiv.id='calendar';
    oDiv.style.width="100%";
    oDiv.style.height="218px";
    oDiv.style.borderTop="1px solid #ccc";
    oDiv.style.overflowX="hidden;";
    oDiv.style.zIndex="999";
    oDiv.style.position='fixed';
    oDiv.style.backgroundColor='rgba(250,250,250,0)';
    oDiv.style.top= '0px';
    document.body.appendChild(oDiv);
}
var hides = '';
function show(hidediv,dateStr,getId,obj0,obj1,falgs){
    hide();
    $("#"+hidediv).hide();
    hides = hidediv;
    createDiv();
    valueId = dateStr;
    minDate = obj0;
    maxDate = obj1;
    whereId= getId;
    falg = falgs;
    valueDate =document.getElementById(getId).value;
    bindDate(valueDate);
}

function getValue(obj,dateStr,getId){
    var value = document.getElementById(obj).value;
    if(value!=undefined){
        if(value.length == 8){
            document.getElementById(getId).value = value;
            if(falg == ''){
                document.getElementById(dateStr).innerHTML = value.substring(0,4)+"-"+value.substring(4,6)+"-"+value.substring(6,8);
            }else{
                if(falg == '0'){
                    document.getElementById(dateStr).innerHTML = value.substring(0,4)+"-"+value.substring(4,6)+"-"+value.substring(6,8);
                    $("#week").text(showDay(value));
                }else{
                    $("#day"+falg).text(value.substring(6,8)+"");
                    $("#time"+falg).text(value.substring(0,4)+"/"+value.substring(4,6));
                    $("#week"+falg).text(showDay(value));
                }
            }
            hide();
            $("#"+hides).show();
        }
    }
}
function hide(){
    var calendar = document.getElementById('calendar');
    if(calendar){
        calendar.parentNode.removeChild(calendar);
    }
}
function header(){
    var str = '<table cellSpacing="2" cellPadding="0" border="0" width="100%" height="40px" style="border-bottom:2px solid #5a5f68;color: #fff;font-size: 20px;background-color:#e72b2b;"><tr align="center">';
    str += '<td id="previousMonth" title="上一月份" style="cursor:pointer;width:60px;font-weight:bold;" onclick="changeMonth(false);"><<</td>';
    str += '<td id="currentYear" style="font-size:16px;font-weight:blod;">0</td>';
    str += '<td id="nextMonth" title="下一月份" style="cursor:pointer;width:60px;" onclick="changeMonth(true);">>></td>';
    str += '</table>';
    return str;
}
function body(row){
    var n = 0;
    var str = '<table border="0" id="bodyBar" cellSpacing="0" cellPadding="0" width="100%" style="border-collapse:collapse;font-size:1em;">';
    var day = new Array('一','二','三','四','五','六','日');
    str += '<tr align="center" style="height:30px;color:#919396;background-color:#e3e6ec;">';
    for(j = 0; j < 7; j++){
        str += '<td style="border-right: solid #ccc 1px;font-weight:bold;">' + day[j] + '</td>';
    }
    str += '</tr>';
    for(i = 0; i < row; i++){
        str += '<tr align="center">';
        for(j = 0; j < 7; j++){
            str += '<td id="cdrDay'+(n)+'" style="border: solid #ccc 1px;height:44px;font-size:16px;background-color:#FFF;" onclick="getValue(\'cdrDay'+(n)+'\',\''+valueId+'\',\''+whereId+'\')" width="13%"></td>';
            n++;
        }
        str += '</tr>';
    }
    str += '</table>';
    return str;
}

function holiday(date){
    return (date.getDay() == 6 || date.getDay() == 0);
}

function bindDate(d){
    var _monthDays = new Array(31,30,31,30,31,30,31,31,30,31,30,31);
    if(d.length == 8){
        d = d.substring(0,4)+"-"+d.substring(4,6)+"-"+d.substring(6,8);
    }
    var _arr = d.split('-');
    var _date = new Date(_arr[0],_arr[1]-1,_arr[2]);
    if(isNaN(_date)) _date = new Date();
    dates = _date;
    //文本框输入值
    var _year = _date.getFullYear();
    var _month = _date.getMonth();
    var _day = 1;

    //这个年月1号是星期几
    var _startDay = new Date(_year,_month,0).getDay();
    //下一年
    var _previYear = _month == 0 ? _year - 1 : _year;
    //下一月
    var _previMonth = _month == 0 ? 11 : _month - 1;
    //这个月中的天数
    var _previDay = _monthDays[_previMonth];
    //平闰年判断
    if (_previMonth == 1)
        _previDay =((_previYear%4==0)&&(_previYear%100!=0)||(_previYear%400==0))?29:28;

    _previDay -= _startDay - 1;
    var _nextDay = 1;
    _monthDays[1] = ((_year%4==0)&&(_year%100!=0)||(_year%400==0))?29:28;

    var row = (_startDay < 5) ? 5 : 6;
    document.getElementById("calendar").innerHTML = header() + body(row);
    bindHeader(_year,_month+1);
    var curDay0=0,curDay1=0,curDay2=0,curDay3=0;
    curDay0 = valueDate.replace('-','').replace('-','');
    if(minDate != ''){
        var other = new Date();
        other.setDate(curDate.getDate()+ parseInt(minDate));
        curDay2 = paseDate(other.getFullYear(),(other.getMonth()+1),other.getDate());
    }
    if(maxDate != ''){
        var other = new Date();
        other.setDate(curDate.getDate()+ parseInt(maxDate));
        curDay3 = paseDate(other.getFullYear(),(other.getMonth()+1),other.getDate());
    }
    for(i = 0; i < row*7; i++){
        //设定每个日期上的事件
        var _dayElement = document.getElementById("cdrDay"+i);
        _dayElement.style.color = "#ccc";
        curDay1 = paseDate(_year,(_month + 1),_day);
        if(i >= _startDay && _day <= _monthDays[_month]){//本月
            if(curDay2 > 0 && curDay3 > 0){
                if(curDay1 > curDay2){
                    if(curDay1 < curDay3){
                        _dayElement.style.color = "#000";
                        _dayElement.value = paseDate(_year,(_month + 1),_day);
                        if(holiday(new Date(_year,_month,_day))){
                            _dayElement.style.color = "#8b95a1";
                        }
                    }
                }
            }else if(curDay2 > 0 && curDay3 == 0){
                if(curDay1 > curDay2){
                    _dayElement.style.color = "#000";
                    _dayElement.value = paseDate(_year,(_month + 1),_day);
                    if(holiday(new Date(_year,_month,_day))){
                        _dayElement.style.color = "#8b95a1";
                    }
                }
            }else if(curDay2 == 0 && curDay3 > 0){
                if(curDay1 < curDay3){
                    _dayElement.style.color = "#000";
                    _dayElement.value = paseDate(_year,(_month + 1),_day);
                    if(holiday(new Date(_year,_month,_day))){
                        _dayElement.style.color = "#8b95a1";
                    }
                }
            }else{
                _dayElement.style.color = "#000";
                _dayElement.value = paseDate(_year,(_month + 1),_day);
                if(holiday(new Date(_year,_month,_day))){
                    _dayElement.style.color = "#8b95a1";
                }
            }
            if(curDay0 == curDay1){
                _dayElement.style.color = "#FFF";
                _dayElement.style.backgroundColor = "#8b95a1";
            }
            _dayElement.innerHTML = _day;
            _day++;
        }else if(i < _startDay){//上个月
            //var _previDate = new Date(_year,_month - 1,_previDay);
            _dayElement.innerHTML = _previDay;
            //_dayElement.value = paseDate(_previDate.getFullYear(),(_previDate.getMonth() + 1),_previDay);
            _previDay++;
        }else if(_day > _monthDays[_month]){//下个月
//            var _nextDate = new Date(_year,_month + 1,_nextDay);
            _dayElement.innerHTML = _nextDay;
//            _dayElement.style.color = "#000";
//            _dayElement.value = paseDate(_nextDate.getFullYear(),(_nextDate.getMonth() + 1), _nextDay);
            _nextDay++;
        }
    }
}

function bindHeader(year,month){
    var _curYear = document.getElementById("currentYear");
    _curYear.innerHTML = year+"年"+month+"月"
}
function changeMonth(isnext){
    var _year = dates.getFullYear();
    var _month = dates.getMonth() + 1;
    var _date = 1;
    if(isnext){
        _month ++;
    } else {
        _month--;
    }
    if(_year > 999 && _year <10000){
        if(_month < 1) {
            _month = 12;
            _year--;
        }
        if(_month > 12) {
            _month = 1;
            _year++;
        }
    }
    if(_month < 10){
        _month = "0" + _month;
    }
    if(_date < 10){
        _date = "0" + _date;
    }
    bindDate(_year + '-' + _month + '-' + _date);
}
function paseDate(year,month,date){
    if(month < 10){
        month = "0" + month;
    }
    if(date < 10){
        date = "0" + date;
    }
    return year + "" + month + "" + date;
}

function showDay(value){
    var day = new Array('星期日','星期一','星期二','星期三','星期四','星期五','星期六');
    return day[new Date(value.substring(0,4),value.substring(4,6)-1,value.substring(6,8)).getDay()];
}